package com.study.ming.studydemo.thd;

//todo 模拟一个医生护士照顾病人的场景
public class StudyWaite2 {
    public static void main(String[] args) throws InterruptedException {
        book b1 = new book();
        myThread1 t1 = new myThread1(b1);
        myThread2 t2 = new myThread2(b1);
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        t2.start();
    }
}

class myThread1 extends Thread {
    book b;

    public myThread1(book b) {
        super();
        this.b = b;
    }

    @Override
    public void run() {
        while (true) {
            b.addbook();
            try {
                System.out.println("t1睡觉了哦");
                Thread.sleep(5000);
                System.out.println("t1醒了呐");
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

//线程类2
class myThread2 extends Thread {
    book b;

    public myThread2(book b) {
        this.b = b;
    }

    @Override
    public void run() {
        while (true) {
            b.delbook();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

//资源类
class book {
    public int number;

    //增加数据
    public synchronized void addbook() {
        System.out.println(Thread.currentThread().getName() + "------" + (++number));
        ;
    }

    //减少数据
    public synchronized void delbook() {
        System.out.println(Thread.currentThread().getName() + "------" + (--number));
        ;
    }

}

//————————————————
//        版权声明：本文为CSDN博主「侠客岛主」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//        原文链接：https://blog.csdn.net/tomorrow_shoe/article/details/98679696
class AutoThread extends Thread {

    private final Object lock;
    private String name;

    @Override
    public void run() {
        try {
            synchronized (lock) {
                long start = System.currentTimeMillis();
                System.out.println(name + "开始执行");
                System.out.println(name + "休眠10秒");
//                Thread.sleep(10000);
                System.out.println(name + "结束休眠,开始 waite");
                lock.wait();
//                long end = System.currentTimeMillis();
                System.out.println(name + "结束");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public AutoThread(Object lock, String name) {
        super();
        this.lock = lock;
        this.name = name;
    }

}

class NurseThread extends Thread {
    private final Object lock;
    private String name;

    @Override
    public void run() {
        synchronized (lock) {
            // 业务逻辑完成了...
            System.out.println(name + "开始执行");
            // 做一些业务逻辑相关的事。。。。
            try {
                System.out.println(name + "休眠3秒");
                Thread.sleep(3000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(name + "结束休眠,唤醒所有");
            lock.notifyAll();
            System.out.println(name + "结束");
        }
    }

    public NurseThread(Object lock, String name) {
        super();
        this.lock = lock;
        this.name = name;
    }

}

class DoctorThread extends Thread {

    private final Object lock;
    private String name;

    @Override
    public void run() {
        try {
            synchronized (lock) {
                long start = System.currentTimeMillis();
                System.out.println(name + "开始执行");
                System.out.println(name + "休眠3秒");
                Thread.sleep(3000);
                System.out.println(name + "结束休眠,开始 waite");
                lock.wait();
                long end = System.currentTimeMillis();
                System.out.println(name + "结束");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public DoctorThread(Object lock, String name) {
        super();
        this.lock = lock;
        this.name = name;
    }

}
